package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.AddressParser;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.adapter.ArrayListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Description 市界面
 * @author jaily.zhang
 * @date 2015-4-9 下午3:25:03
 * @version V1.3.1
 */
public class CityInfoActivity extends BaseActivity implements OnClickListener {

	private AddressParser parser;
	private List<String> showCitys;
	private Context context;
	private ImageView regist_back;
	private TextView tv_headtitle;
	private ListView lv_mylist;
	private String city;
	private List<String> addrString;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.provinceinfo_layout);
		addrString=new ArrayList<>();
		addrString.add("北京市");
		addrString.add("上海市");
		addrString.add("天津市");
		addrString.add("重庆市");

		context=this;
		parser = new AddressParser(context);
		city=getIntent().getStringExtra("city");
		showCitys = parser.showCitys(city);
		initTitle();
		initView();
	}

	/**
	 * 
	 * @Description 初始化title
	 * @author jaily.zhang
	 */
	private void initTitle() {
		regist_back=(ImageView) findViewById(R.id.regist_back);
		regist_back.setOnClickListener(this);
		tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
		tv_headtitle.setText("市");
		lv_mylist=(ListView) findViewById(R.id.lv_mylist);
		lv_mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent=new Intent(context,DistinctInfoActivity.class);
				intent.putExtra("distinct",showCitys.get(position));
				if(addrString.contains(city))
				{
					intent.putExtra("addr",city);
				}else {
					intent.putExtra("addr",city+showCitys.get(position));
				}
				startActivityForResult(intent,0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==0 && resultCode==1)
		{
			setResult(1,data);
			finish();
		}
	}

	/**
	 * 
	 * @Description 初始化界面
	 * @author jaily.zhang
	 */
	private void initView() {
		MyAdapter adapter = new MyAdapter(context);
		adapter.setList(showCitys);
		lv_mylist.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.regist_back:
				finish();
				break;
		}
	}

	class MyAdapter extends ArrayListAdapter<String> {

		private LayoutInflater inflater;
		private Context mContext;

		public MyAdapter(Context context) {
			super(context);
			this.mContext = context;
			this.inflater = LayoutInflater.from(mContext);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.item_address_province, null);
			TextView province = (TextView) convertView.findViewById(R.id.province);
			province.setText(getList().get(position) + "");
			return convertView;
		}

	}
	
}
